class EnemyMelee extends EnemyCharacter
{
	private var _enrage:boolean;
	private var _origAttack:int = 15;
	private var _origActualDamage:int = 15;
	
	private var _enrageAttack:int = 30;
	private var _enrageActualDamage:int = 30;
	private var _enragedEffect;
	
	public function EnemyMelee()
	{		
		
		toDisplayOverlayText = false;
				
		turnsLeft = MAX_TURNS;
		
		isExecuting = false;
	
		_visible = true;
		_hasMoved = true;
		_path = new Array(0);
		_destReached = true;
		_destTile = null;
		characterName = "Enemy";
		
		//Attack
		attackRadius = 2;
		engageRadius = 5; // less than move radius, more than attack radius
		attackDamage = _origAttack;
		actualDamage = _origActualDamage;
		_attackPercent = 50.0;
		
		//Sight
		sightRadius = 8;
		moveRadius = 6;
		
		_isSelectable = false;
		_attackableText = null;
		
		//Health Status
		maxHealth = 150;
		myHealth = maxHealth;

		//For diplaying mouse over
		healthPercent = 100.0f;
		healthBarLength = Screen.width/9;
		style = new GUIStyle();
	    texture = new Texture2D(1, 1);
		
		//Enemy type
		_enemyType = EnemyType.ENEMY_MELEE;
		
		_enragedEffect = null;		
		
	}
	
	public function attack(target:GameCharacter, projectileType:int)
	{
		super.attack(target, projectileType);
		
		//calcualte hit chance
		var hitChance = calculateHitChance(target);
		
		//play attack animation
		setAnimationState(ATTACK_ANI);
		
		//fire bullet
		var bullet = Resources.Load("Projectile_melee");
		bullet = Instantiate(bullet);
		var projectile = bullet.GetComponent(Projectile);
		
		//random attack chance
		if (Random.Range(0,100) < hitChance)
			//hit
			projectile.setup(this, target, true, projectileType);
		else
			//miss
			projectile.setup(this, target, false, projectileType);
			
		Destroy(bullet,5);
		isLocked = false;
		if (projectileType == Projectile.NORMAL)
			endTurn();
	}
	
	private function attackWithHitChance(target:GameCharacter, hitChance:int)
	{
		super.attack(target, Projectile.NORMAL);
		
		//fire bullet
		var bullet = Resources.Load("Projectile_Arrow");
		bullet = Instantiate(bullet);
		var projectile = bullet.GetComponent(Projectile);
		
		//random attack chance
		if (Random.Range(0,100) < hitChance)
			//hit
			projectile.setup(this, target, true, Projectile.NORMAL);
		else
			//miss
			projectile.setup(this, target, false, Projectile.NORMAL);
			
		Destroy(bullet,5);
		isLocked = false;
		endTurn();
	}
	
	//OVERRIDE THIS FUNCTION IN EVERY INHERITED CLASS
	public function calculateHitChance(target:GameCharacter)
	{
		//currently calculating based on an average atk
		//calculate distance of enemy
		var distance = Vector3.Distance(target.getSurfaceCoord(), this.getSurfaceCoord());
		//based on the maximum range of weapon, calculate the hit chance based on the optimized range
		//close - mid range weapon
		var optimizedRangeMax = 5;
		var hitChance = 100;
		//too far
		if (distance > optimizedRangeMax)
			hitChance -= (distance - optimizedRangeMax) * 20;
		//if in cover
		if (target.isInCover())
		{
			if (!target.isFlanked(this))
				hitChance -= Obstacle.HIT_CHANCE_REDUCTION;
		}
		
		//check height advantage
		var heightDifference = target.getSurfaceCoord().y - this.getSurfaceCoord().y;
		if (heightDifference > 0)
			hitChance -= heightDifference * 10;
		else if (heightDifference < 0)
			hitChance += heightDifference * -10;
		
		//minimum 10%
		if (hitChance < 10)
			hitChance = 10;
		else if (hitChance > 100)
			hitChance = 100;
		return hitChance;
	}
	
	public function calculateActualDamage(target:GameCharacter)
	{
		//if in cover
		if (target.isInCover())
		{
			//check whether flanked
			if (target.isFlanked(this))
			{
				return attackDamage *2; 
			}
			else
			{
				return attackDamage;
			}
		}
		//not in cover
		else
		{
			return attackDamage *2;
		}
	}
	
	public function skill(target:GameCharacter) // Charge and attack with high damage
	{		
		if(!_isCooldown){
			setAnimationState(SKILL_ANI);
			startCooldown();
			_enrage = true;
			
			attackDamage = _enrageAttack;
			actualDamage = _enrageActualDamage;
			
			/*var enragedEffect = Resources.Load("EnragedParticle");
			var enraged = MonoBehaviour.Instantiate(enragedEffect);
			enraged.transform.position = this.transform.position;
			Destroy(enraged, 5);*/
			
			var enragedEffect = Resources.Load("EnragedParticle");
			_enragedEffect = MonoBehaviour.Instantiate(enragedEffect);
			_enragedEffect.transform.position = this.transform.position;
			
			adjustCurrentHealth(25);
			
			attackWithHitChance(target, 100);			
			
			Debug.Log("Enraged");
		}
	}
	
	//@Override resetTurn
	public function resetTurn(){
		super.resetTurn();
		
		if(_enrage){
			_enrage = false;
			
			attackDamage = _origAttack;
			actualDamage = _origActualDamage;
			
			Destroy(_enragedEffect);
			
			Debug.Log("Deraged");
		}
	}	
}